package com.feeyo.monitor.rest;

import com.feeyo.monitor.domain.Crossing;
import com.feeyo.monitor.domain.LimitArea;
import com.feeyo.monitor.service.CrossingService;
import com.feeyo.monitor.service.UserVehiclePermissionService;
import com.feeyo.monitor.utils.GloabalConstant;
import com.feeyo.monitor.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author <a href="mailto:bliulei@variflight.com">liulei</a>
 * @Description Created by liulei on 2017/8/28 下午5:24.
 */
@RestController
@RequestMapping("/monitor")
public class CrossingController {
    @Autowired
    CrossingService crossingService;
    @Autowired
    UserVehiclePermissionService userVehiclePermissionService;


    @RequestMapping(value="/crossings", method= RequestMethod.GET)
    public ResultData<List<Crossing>> getCrossings(@RequestParam(value="userId",required=true) String userId){
        ResultData<List<Crossing>> rd = new ResultData<List<Crossing>>();
        Map perm = userVehiclePermissionService.selectByUserId(userId);
        String airport = (String)perm.get("airport");
        if (airport != null){
            String[] arr = airport.split(",");
            airport = arr[0];
        }
        List<Crossing> result = null;
        try{
            result = crossingService.selectByAirport(airport);
        }catch (Exception e){
            rd.setCode(GloabalConstant.FAILED);
            rd.setMessage(e.getMessage());
            return rd;
        }

        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(result);
        return rd;
    }

    /**
     * @Description 新建/修改 道口
     * @params
     * @return
     */
    @RequestMapping(value="/crossing", method=RequestMethod.POST)
    public ResultData<Object> saveCrossing(@RequestParam(value="id",required=false)String id,
                                            @RequestParam(value="name",required=true)String name,
                                            @RequestParam(value="power",required=true)String power,
                                            @RequestParam(value="airport",required=true)String airport,
                                            @RequestParam(value="line",required=true)String line,
                                            @RequestParam(value="visible",required=true)Boolean visible){
        ResultData<Object> rd = new ResultData<Object>();
        String [] lineArr = line.split(";");
        StringBuilder sb = new StringBuilder();
        sb.append("LINESTRING(");
        for(String lonlat : lineArr){
            sb.append(lonlat.replace(",", " ") + ",");
        }
        sb.setLength(sb.length()-1);
        sb.append(")");
        Crossing crossing = new Crossing();
        crossing.setId(id);
        crossing.setLine(sb.toString());
        crossing.setName(name);
        crossing.setPower(power);
        crossing.setAirport(airport);
        crossing.setVisible(visible);
        try{
            if (id == null)
                crossingService.insert(crossing);
            else
                crossingService.updateByPrimaryKeySelective(crossing);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(crossing);
        return rd;
    }

    /**
     * @Description 道口详细信息
     * @params
     * @return
     */
    @RequestMapping(value="/crossing/{id}", method=RequestMethod.GET)
    public ResultData<Crossing> getCrossing(@PathVariable("id")String id){
        ResultData<Crossing> rd = new ResultData<Crossing>();
        Crossing result = null;
        try{
            result = crossingService.selectByPrimaryKey(id);
        }catch (Exception e){
            rd.setCode(GloabalConstant.FAILED);
            rd.setMessage(e.getMessage());
            return rd;
        }

        rd.setCode(GloabalConstant.SUCCESS);
        rd.setData(result);
        return rd;
    }

    /**
     * @Description 删除道口
     * @params
     * @return
     */
    @RequestMapping(value="/crossing/delete/{id}", method=RequestMethod.GET)
    public ResultData<Object> delete(@PathVariable("id")String id){
        ResultData<Object> rd = new ResultData<Object>();
        try{
            crossingService.updateDeleteStatus(id);
        }catch(Exception e){
            rd.setCode(-1);
            rd.setMessage(e.getMessage());
            e.printStackTrace();
            return rd;
        }
        rd.setCode(GloabalConstant.SUCCESS);
        return rd;
    }
}
